import { Controller, Get, Post, Body, Patch, Param, Delete, BadRequestException, UseGuards } from '@nestjs/common'
import { AuthGuard } from '@nestjs/passport'
import { ModeService } from './mode.service'
import { CreateModeDto } from './dto/create-mode.dto'
import { UpdateModeDto } from './dto/update-mode.dto'
import { Roles } from 'src/auth/roles.decorator'
import { RolesGuard } from 'src/auth/roles.guard'

@Controller('mode')
@UseGuards(AuthGuard('jwt'))
export class ModeController {
  constructor(private readonly modeService: ModeService) { }

  @Post()
  @Roles('admin')
  @UseGuards(RolesGuard)
  create(@Body() createModeDto: CreateModeDto) {
    return this.modeService.create(createModeDto)
  }

  @Get()
  @Roles('admin', 'normal')
  @UseGuards(RolesGuard)
  findAll() {
    return this.modeService.findAll()
  }

  @Get(':id')
  @Roles('admin', 'normal')
  @UseGuards(RolesGuard)
  async findOne(@Param('id') id: string) {
    const result = await this.modeService.findOne(+id)
    if (!result) {
      throw new BadRequestException("未找到数据")
    }
    return result
  }

  @Patch(':id')
  @Roles('admin')
  @UseGuards(RolesGuard)
  update(@Param('id') id: string, @Body() updateModeDto: UpdateModeDto) {
    return this.modeService.update(+id, updateModeDto)
  }

  @Delete(':id')
  @Roles('admin')
  @UseGuards(RolesGuard)
  remove(@Param('id') id: string) {
    return this.modeService.remove(+id)
  }
}
